package chinaums

import (
	"time"
	"thirdpaysdk/utils/mapstruct"
	//"github.com/astaxie/beego"
)

/*
{"msgType":"wx.orderQuery","payTime":"2019-01-25 14:33:06","connectSys":"UNIONPAY","errMsg":"查询成功","merName":"北京大黄蜂信息科技有限公司","mid":"898440057340002","invoiceAmount":1,"settleDate":"2019-01-25","billFunds":"现金:1","buyerId":"otdJ_uDgiA2201QkqVzLYBUeSMLw","tid":"99113893","couponAmount":0,"targetMid":"270415091","cardAttr":"CREDIT_CARD","targetOrderId":"4200000248201901251916621014","billFundsDesc":"现金支付0.01元。","subBuyerId":"oQlEQ0RqFx7ufQUbkrNB8xouAgDI","targetStatus":"SUCCESS","seqId":"06142190162N","merOrderId":"501380077866962944","refundAmount":0,"targetSys":"WXPay","bankInfo":"平安银行(信用卡)","msgSrc":"WWW.BJDHFXX.COM","settleRefId":"06142190162N","totalAmount":1,"responseTimestamp":"2019-01-26 10:21:40","errCode":"SUCCESS","buyerPayAmount":1,"status":"TRADE_SUCCESS","sign":"BC0B0DAAA3BD860F33BAC21AE0E4C737"}
*/

type OrderQueryResponse struct {
	ErrCode           string         `json:"errCode"` 
	MsgSrc            string         `json:"msgSrc"`
	ResponseTimestamp string         `json:"responseTimestamp"` 
	Mid               string         `json:"mid,omitempty"`
	Tid               string         `json:"tid,omitempty"`
	//
	MsgType           string         `json:"msgType,omitempty"`
	ErrMsg            string         `json:"errMsg,omitempty"` 
	MerName           string         `json:"merName,omitempty"`
	MerOrderId        string         `json:"merOrderId,omitempty"`
	SeqId             string         `json:"seqId,omitempty"` 
	SettleRefId       string         `json:"settleRefId,omitempty"`
	Status            string         `json:"status,omitempty"`
	TotalAmount       int            `json:"totalAmount,omitempty"`
	TargetOrderId     string         `json:"targetOrderId,omitempty"` 
	TargetSys         string         `json:"targetSys,omitempty"`
	TargetStatus      string         `json:"targetStatus,omitempty"` 
	TargetMid         string         `json:"targetMid,omitempty"` 
	Sign              string         `json:"sign"`
}

func OrderQuery(mid,tid,msgSrc,secreCode,orderId string)(rsp *OrderQueryResponse, err error){
	type Request struct {
		MsgSrc           string  `json:"msgSrc"`
		MsgType          string  `json:"msgType"`
		RequestTimestamp string  `json:"requestTimestamp"`
		MerOrderId       string  `json:"merOrderId"`
		Mid              string  `json:"mid"`
		Tid              string  `json:"tid"`
		InstMid          string  `json:"instMid"`
		Sign             string  `json:"sign"`
	}
	var request Request
	request.MsgSrc = msgSrc
	request.MsgType = "query"
	request.RequestTimestamp = time.Now().Format("2006-01-02 15:04:05")
	request.MerOrderId = orderId
	request.Mid = mid
	request.Tid = tid
	request.InstMid = "MINIDEFAULT"
	reqParams := mapstruct.Struct2Map(request, "json")
	reqURL := "https://qr.chinaums.com/netpay-route-server/api/"
	sdk := NewSDK(reqURL, secreCode)
	resp, e := sdk.RequestOther(reqParams)
	if e != nil {
		err = e
		return
	}
	rsp = new(OrderQueryResponse)
	err = mapstruct.Map2Struct(resp,rsp,"json")
	return
}
